#ifndef _UI_NOTE_H
#define _UI_NOTE_H

static lv_obj_t		*uiNote;
static lv_obj_t		*uiNoteTitle;
static lv_label_t	*uiNoteTitleLabel;
static lv_label_t	*uiNoteContentLabel;
static lv_obj_t		*uiNoteBtn_0;
static lv_obj_t		*uiNoteBtn_1;
static lv_obj_t		*uiNoteBtn_2;
static lv_label_t	*uiNoteBtnLabel_0;
static lv_label_t	*uiNoteBtnLabel_1;
static lv_label_t	*uiNoteBtnLabel_2;
static lv_style_t	btnStyleNormal;
static lv_style_t	btnStyleFocus;
static lv_style_t	btnStylePressed;
static lv_btn_t		*btnGroup[3];
static int btnCnt 	= 0;
static int currentBtnNum = 0;

const static int uiNoteWidth		= CONFIG_SCREEN_WIDTH - 50;
const static int uiNoteHeight		= CONFIG_SCREEN_HEIGHT - 60;
const static int uiNoteTitleHeight	= 24;
const static int uiNoteBottomHeight	= 40;
const static int uiNoteRadius		= 10;
const static int uiNoteButtonHeight	= 35;
const static int uiNoteButtonWidth	= 75;

static void (*uiNoteBtnClickFun_0)();
static void (*uiNoteBtnClickFun_1)();
static void (*uiNoteBtnClickFun_2)();

static void uiNoteRefBtnState() {
	lv_obj_clear_state(uiNoteBtn_0, LV_STATE_FOCUSED | LV_STATE_PRESSED);
	lv_obj_clear_state(uiNoteBtn_1, LV_STATE_FOCUSED | LV_STATE_PRESSED);
	lv_obj_clear_state(uiNoteBtn_2, LV_STATE_FOCUSED | LV_STATE_PRESSED);
	lv_obj_add_state(btnGroup[currentBtnNum], LV_STATE_FOCUSED);
}

void hideNote() {
	rotFunGoBack();
	lv_obj_add_flag(uiNote, LV_OBJ_FLAG_HIDDEN);
}

static void uiNoteBtnCb_0(lv_event_t *e) {
	lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_PRESSED) {
		// log("btn 0 LV_EVENT_PRESSED");
		uiNoteBtnClickFun_0();
		DELAY(100);
		uiNoteRefBtnState();
		DELAY(50);
		hideNote();
	}
	
}
static void uiNoteBtnCb_1(lv_event_t *e) {
	lv_event_code_t code = lv_event_get_code(e);
	if(code == LV_EVENT_PRESSED) {
		// log("btn 1 LV_EVENT_PRESSED");
		uiNoteBtnClickFun_1();
		DELAY(100);
		uiNoteRefBtnState();
		DELAY(50);
		hideNote();
	}
}
static void uiNoteBtnCb_2(lv_event_t *e) {
	lv_event_code_t code = lv_event_get_code(e);
	if(code == LV_EVENT_PRESSED) {
		// log("btn 2 LV_EVENT_PRESSED");
		uiNoteBtnClickFun_2();
		DELAY(100);
		uiNoteRefBtnState();
		DELAY(50);
		hideNote();
	}
}




static void uiNoteBtnTurn(bool s) {
	if(s) {
		currentBtnNum++;
		if(currentBtnNum >= btnCnt) {
			// currentBtnNum = 0;
			currentBtnNum = btnCnt - 1;
		}
	}
	else {
		currentBtnNum--;
		if(currentBtnNum < 0) {
			// currentBtnNum = btnCnt - 1;
			currentBtnNum = 0;
		}
	}
	uiNoteRefBtnState();
}

static void uiNoteRotFunA() {
	uiNoteBtnTurn(true);
}

static void uiNoteRotFunB() {
	uiNoteBtnTurn(false);
}

static void uiNoteRotFunC() {
	lv_event_send(btnGroup[currentBtnNum], LV_EVENT_PRESSED, NULL);
}

void showNote(note_t n) {
	btnCnt = 0;
	lv_obj_set_style_bg_color(uiNoteTitle, n.bgColor, 0);
	lv_label_set_text(uiNoteTitleLabel, n.title);
	lv_label_set_text(uiNoteContentLabel, n.content);
	
	if(n.enable_0) {
		btnCnt++;
		lv_label_set_text(uiNoteBtnLabel_0, n.text_0);
		lv_obj_add_event_cb(uiNoteBtnLabel_0, n.fun_0, LV_EVENT_CLICKED, NULL);
		uiNoteBtnClickFun_0 = n.fun_0;
		lv_obj_clear_flag(uiNoteBtn_0, LV_OBJ_FLAG_HIDDEN);
	}
	else {
		lv_obj_add_flag(uiNoteBtn_0, LV_OBJ_FLAG_HIDDEN);
	}
	if(n.enable_1) {
		btnCnt++;
		lv_label_set_text(uiNoteBtnLabel_1, n.text_1);
		lv_obj_add_event_cb(uiNoteBtnLabel_1, n.fun_1, LV_EVENT_CLICKED, NULL);
		uiNoteBtnClickFun_1 = n.fun_1;
		lv_obj_clear_flag(uiNoteBtn_1, LV_OBJ_FLAG_HIDDEN);
	}
	else {
		lv_obj_add_flag(uiNoteBtn_1, LV_OBJ_FLAG_HIDDEN);
	}
	if(n.enable_2) {
		btnCnt++;
		lv_label_set_text(uiNoteBtnLabel_2, n.text_2);
		lv_obj_add_event_cb(uiNoteBtnLabel_2, n.fun_2, LV_EVENT_CLICKED, NULL);
		uiNoteBtnClickFun_2 = n.fun_2;
		lv_obj_clear_flag(uiNoteBtn_2, LV_OBJ_FLAG_HIDDEN);
	}
	else {
		lv_obj_add_flag(uiNoteBtn_2, LV_OBJ_FLAG_HIDDEN);
	}
	rotFunSet(&uiNoteRotFunA, &uiNoteRotFunB, &uiNoteRotFunC);
	
	lv_obj_move_foreground(uiNote);
	lv_obj_clear_flag(uiNote, LV_OBJ_FLAG_HIDDEN);
	uiNoteRefBtnState();
}


void testA() {
	log("testA");
}
void testB() {
	log("testB");
}
void testC() {
	log("testC");
}

void testNote() {
	note_t note = {
		.bgColor = LV_COLOR_ERROR_1,
		.title = "警告",
		.content = "连接失败请检查WIFI的SSID和密码是否正确。或者重新连接再试一次。再试一次。",
		.enable_0 = true,
		.text_0 = "确定",
		.fun_0 = &testA,
		.enable_1 = true,
		.text_1 = "取消",
		.fun_1 = &testB,
		// .enable_2 = true,
		// .text_2 = "关闭网络",
		// .fun_2 = &testC,
	};
	showNote(note);
}


void uiNoteInit() {
	LVMT;
	lv_style_init(&btnStyleNormal);
	lv_style_set_width(&btnStyleNormal, uiNoteButtonWidth);
	lv_style_set_height(&btnStyleNormal, uiNoteButtonHeight);
	lv_style_set_radius(&btnStyleNormal, LV_RADIUS_CIRCLE);
	lv_style_set_bg_color(&btnStyleNormal, LV_COLOR_BTN_BG_2);
	// lv_style_set_text_color(&btnStyleNormal, LV_COLOR_MAIN_BG_2);
	lv_style_set_text_color(&btnStyleNormal, LV_COLOR_WHITE);

	lv_style_init(&btnStyleFocus);
	lv_style_set_width(&btnStyleFocus, uiNoteButtonWidth);
	lv_style_set_height(&btnStyleFocus, uiNoteButtonHeight);
	lv_style_set_radius(&btnStyleFocus, LV_RADIUS_CIRCLE);
	lv_style_set_bg_color(&btnStyleFocus, LV_COLOR_BTN_BG_1);
	lv_style_set_text_color(&btnStyleNormal, LV_COLOR_WHITE);

	lv_style_init(&btnStylePressed);
	lv_style_set_width(&btnStylePressed, uiNoteButtonWidth);
	lv_style_set_height(&btnStylePressed, uiNoteButtonHeight);
	lv_style_set_radius(&btnStylePressed, LV_RADIUS_CIRCLE);
	lv_style_set_bg_color(&btnStylePressed, LV_COLOR_MAIN_BG_4);
	lv_style_set_shadow_opa(&btnStylePressed, LV_OPA_0);
	lv_style_set_text_color(&btnStyleNormal, LV_COLOR_WHITE);

	uiNote = lv_obj_create(root);
	lv_obj_add_flag(uiNote, LV_OBJ_FLAG_HIDDEN);
	lv_obj_remove_style_all(uiNote);
	lv_obj_set_style_bg_color(uiNote, LV_COLOR_BLACK, 0);
	lv_obj_set_style_bg_opa(uiNote, LV_OPA_70, 0);
	lv_obj_set_size(uiNote, CONFIG_SCREEN_WIDTH, CONFIG_SCREEN_HEIGHT);
	lv_obj_center(uiNote);
	
	lv_obj_t *uiNoteBody = lv_obj_create(uiNote);
	lv_obj_remove_style_all(uiNoteBody);
	lv_obj_set_size(uiNoteBody, uiNoteWidth, uiNoteHeight);
	lv_obj_center(uiNoteBody);

	uiNoteTitle = lv_obj_create(uiNoteBody);
	lv_obj_remove_style_all(uiNoteTitle);
	lv_obj_set_style_bg_color(uiNoteTitle, LV_COLOR_MAIN_BG_1, 0);
	lv_obj_set_style_bg_opa(uiNoteTitle, LV_OPA_COVER, 0);
	lv_obj_set_style_radius(uiNoteTitle, uiNoteRadius, 0);
	lv_obj_set_size(uiNoteTitle, uiNoteWidth, uiNoteTitleHeight + uiNoteRadius);
	lv_obj_set_pos(uiNoteTitle, 0, 0);

	uiNoteTitleLabel = lv_label_create(uiNoteTitle);
	lv_label_set_text(uiNoteTitleLabel, "");
	lv_obj_remove_style_all(uiNoteTitleLabel);
	lv_obj_set_size(uiNoteTitleLabel, uiNoteWidth, LV_SIZE_CONTENT);
	lv_obj_set_pos(uiNoteTitleLabel, 0,  uiNoteTitleHeight / 2 - 8);
	lv_obj_set_style_text_align(uiNoteTitleLabel, LV_TEXT_ALIGN_CENTER, 0);
	lv_obj_set_style_text_color(uiNoteTitleLabel, LV_COLOR_WHITE, 0);
	lv_obj_set_style_text_font(uiNoteTitleLabel, CFONT, 0);
	
	
	lv_obj_t *uiNoteContent = lv_obj_create(uiNoteBody);
	lv_obj_remove_style_all(uiNoteContent);
	lv_obj_set_style_bg_color(uiNoteContent, LV_COLOR_WHITE, 0);
	lv_obj_set_style_bg_opa(uiNoteContent, LV_OPA_COVER, 0);
	lv_obj_set_pos(uiNoteContent, 0, uiNoteTitleHeight);
	lv_obj_set_size(uiNoteContent, uiNoteWidth, uiNoteHeight - uiNoteTitleHeight);
	lv_obj_set_style_radius(uiNoteContent, uiNoteRadius, 0);
	lv_obj_t *uiNoteBlank = lv_obj_create(uiNoteContent);
	lv_obj_remove_style_all(uiNoteBlank);
	lv_obj_set_style_bg_color(uiNoteBlank, LV_COLOR_WHITE, 0);
	lv_obj_set_style_bg_opa(uiNoteBlank, LV_OPA_COVER, 0);
	lv_obj_set_size(uiNoteBlank, uiNoteWidth, uiNoteTitleHeight);
	lv_obj_set_pos(uiNoteBlank, 0, 0);

	int margin = 16;
	uiNoteContentLabel = lv_label_create(uiNoteContent);
	lv_label_set_text(uiNoteContentLabel, "");
	lv_obj_remove_style_all(uiNoteContentLabel);
	lv_obj_set_size(uiNoteContentLabel, uiNoteWidth - margin * 2, uiNoteHeight - uiNoteTitleHeight - uiNoteBottomHeight - margin * 2);
	lv_label_set_long_mode(uiNoteContentLabel, LV_LABEL_LONG_WRAP);
	lv_obj_set_pos(uiNoteContentLabel, margin, margin);
	lv_obj_set_style_bg_opa(uiNoteContentLabel, LV_OPA_COVER, 0);
	lv_obj_set_style_text_font(uiNoteContentLabel, CFONT, 0);
	lv_obj_set_style_text_color(uiNoteContentLabel, LV_COLOR_FONT_1, 0);


	lv_obj_t *uiNoteBottom = lv_obj_create(uiNoteContent);
	lv_obj_remove_style_all(uiNoteBottom);
	lv_obj_set_size(uiNoteBottom, uiNoteWidth - 10, uiNoteBottomHeight);
	lv_obj_set_pos(uiNoteBottom, 5, uiNoteHeight - uiNoteTitleHeight - uiNoteBottomHeight - 10);
	lv_obj_set_style_pad_top(uiNoteBottom, 3, 0);
	lv_obj_set_style_bg_opa(uiNoteBottom, LV_OPA_COVER, 0);
	lv_obj_set_layout(uiNoteBottom, LV_LAYOUT_FLEX);
	lv_obj_set_style_flex_flow(uiNoteBottom, LV_FLEX_FLOW_ROW, 0);
	lv_obj_set_style_flex_main_place(uiNoteBottom, LV_FLEX_ALIGN_SPACE_AROUND, 0);
	lv_obj_set_style_flex_cross_place(uiNoteBottom, LV_FLEX_ALIGN_CENTER, 0);

	uiNoteBtn_0 = lv_btn_create(uiNoteBottom);
	lv_obj_add_style(uiNoteBtn_0, &btnStyleNormal, LV_STATE_DEFAULT);
	lv_obj_add_style(uiNoteBtn_0, &btnStyleFocus, LV_STATE_FOCUSED);
	lv_obj_add_style(uiNoteBtn_0, &btnStylePressed, LV_STATE_PRESSED);
	lv_obj_add_event_cb(uiNoteBtn_0, uiNoteBtnCb_0, LV_EVENT_ALL, NULL);
	uiNoteBtnLabel_0 = lv_label_create(uiNoteBtn_0);
	lv_obj_center(uiNoteBtnLabel_0);
	lv_obj_set_style_text_font(uiNoteBtnLabel_0, CFONT, 0);
	lv_label_set_text(uiNoteBtnLabel_0, "");

	uiNoteBtn_1 = lv_btn_create(uiNoteBottom);
	lv_obj_add_style(uiNoteBtn_1, &btnStyleNormal, LV_STATE_DEFAULT);
	lv_obj_add_style(uiNoteBtn_1, &btnStyleFocus, LV_STATE_FOCUSED);
	lv_obj_add_style(uiNoteBtn_1, &btnStylePressed, LV_STATE_PRESSED);
	lv_obj_add_event_cb(uiNoteBtn_1, uiNoteBtnCb_1, LV_EVENT_ALL, NULL);
	uiNoteBtnLabel_1 = lv_label_create(uiNoteBtn_1);
	lv_obj_center(uiNoteBtnLabel_1);
	lv_obj_set_style_text_font(uiNoteBtnLabel_1, CFONT, 0);
	lv_label_set_text(uiNoteBtnLabel_1, "");

	uiNoteBtn_2 = lv_btn_create(uiNoteBottom);
	lv_obj_add_style(uiNoteBtn_2, &btnStyleNormal, LV_STATE_DEFAULT);
	lv_obj_add_style(uiNoteBtn_2, &btnStyleFocus, LV_STATE_FOCUSED);
	lv_obj_add_style(uiNoteBtn_2, &btnStylePressed, LV_STATE_PRESSED);
	lv_obj_add_event_cb(uiNoteBtn_2, uiNoteBtnCb_2, LV_EVENT_ALL, NULL);
	uiNoteBtnLabel_2 = lv_label_create(uiNoteBtn_2);
	lv_obj_center(uiNoteBtnLabel_2);
	lv_obj_set_style_text_font(uiNoteBtnLabel_2, CFONT, 0);
	lv_label_set_text(uiNoteBtnLabel_2, "");

	LVMG;
	
	btnGroup[0] = uiNoteBtn_0;
	btnGroup[1] = uiNoteBtn_1;
	btnGroup[2] = uiNoteBtn_2;
}



#endif